/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package grest.controller;

import grest.model.DataIO;
import grest.model.StoredProcedure;
import grest.view.VistaEstadisticas;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Agustin
 */
public class CntrlrBtnEstadisticas implements ActionListener {
    VistaEstadisticas form;
    
    public CntrlrBtnEstadisticas(VistaEstadisticas form){
        this.form = form;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==form.getControls("btnResultados")){
            try {
                executeMontoTotal();
            } catch (SQLException ex) {
                System.out.print(ex.getMessage());
            }
        }
    }
    
    private void executeMontoTotal() throws SQLException{
        Boolean ban = false;
        JTable tablaMontoTotal = (JTable)form.getControls("tblMontoTotal");
        String[] headers= {"NRO_MOZO","APELLIDO", "NOMBRE", "TOTAL_VENTAS"};
        DefaultTableModel modelReservas = new DefaultTableModel(headers,0);
        tablaMontoTotal.setModel(modelReservas);
        StoredProcedure procedureMontoTotal = new StoredProcedure("P_MONTO_TOTAL", false);
        java.sql.Date fechaInicio = java.sql.Date.valueOf(((JTextField)form.getControls("txtFechaInicio")).getText());
        java.sql.Date fechaFin = java.sql.Date.valueOf(((JTextField)form.getControls("txtFechaFin")).getText());
        procedureMontoTotal.addParamIn(fechaInicio);
        procedureMontoTotal.addParamIn(fechaFin);
            
        //}
        ResultSet rs =DataIO.executeSProcedure(procedureMontoTotal);
        while (rs.next()){
            ban = true;
            String[] fila = new String[6];
            fila[0]=String.valueOf(rs.getInt("nro_mozo"));
            fila[1]=(rs.getString("apellido"));
            fila[2]=(rs.getString("nombre"));
            fila[3]=String.valueOf(rs.getInt("total_ventas"));
            modelReservas.addRow(fila);
        }
       if (ban == false){
          ((JLabel) form.getControls("lblSinResult")).setVisible(true);
       }
       else{
           ((JLabel) form.getControls("lblSinResult")).setVisible(false);
       }
    }
    
}